<?php

namespace App\Jobs;

use App\Models\VipLogs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class RenewVip implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $vipLog;

    /**
     * Create a new job instance.
     */
    public function __construct($vipLog)
    {
        $this->vipLog = $vipLog;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $vipLog = $this->vipLog;
        Log::info('renew vip: ' . json_encode($vipLog));

        // 创建记录
        $res = VipLogs::query()->create([
            'user_id' => $vipLog['user_id'],
            'payment_id' => null,
            'vip_id' => $vipLog['vip_id'],
            'status' => $vipLog['status'],
            'start_at' => $vipLog['start_at'],
            'end_at' => $vipLog['end_at']
        ]);
        if (!$res) {
            Log::error('renew vip error');
            return;
        }

        Log::info('renew vip success');
    }
}
